﻿namespace WindowsServiceApp
{
    using System.Threading.Tasks;

    using Abp.Quartz.Quartz;

    using Castle.Core.Logging;

    using Quartz;

    public class WriteLogScheduler: IWriteLogScheduler
    {
        public ILogger Logger { get; set; }

        private readonly IQuartzScheduleJobManager _jobManager;

        public WriteLogScheduler(IQuartzScheduleJobManager jobManager)
        {
            this._jobManager = jobManager;
            this.Logger = NullLogger.Instance;
        }

        /// <summary>
        /// 调度Job
        /// </summary>
        /// <returns></returns>
        private async Task ScheduleJob()
        {
            this.Logger.Info("ScheduleJob");
            await this._jobManager.ScheduleAsync<WriteLogJob>(
                job =>
                    {
                    },
                trigger =>
                    {
                        trigger
                            .StartNow()
                            .WithSimpleSchedule(schedule =>
                                {
                                    schedule.RepeatForever()
                                        .WithIntervalInSeconds(5) // 5s执行一次
                                        .Build();
                                });
                    });
        }

        public async Task Start()
        {
            await this.ScheduleJob();
        }

        public async Task Stop()
        {
            this._jobManager.WaitToStop();
            await Task.FromResult(0);
        }
    }

}